package web.login;

import application.MySession;
import application.authorization.RolesSession;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import service.ILoggerService;
import web.cms.CmsBasePage;
import web.commons.LogOffLink;

/**
 * Primary loginpage uses Customername and password.
 */
@MountPath(path = "login")
public final class LoginPage extends CmsBasePage {

    @SpringBean
    private ILoggerService loggerService;
    @SpringBean(name = "mySession")
    private MySession mySession;
    private static final long serialVersionUID = 1L;
    private Panel changePasswordPanel;
    private Panel resetPasswordPanel;
    private BookmarkablePageLink registrationLink;
    private BookmarkablePageLink pageLink;
    private SignPanel signPanel;
    private Link logoff;

    /**
     * Constructor.
     */
    public LoginPage() {

        // stateless so the login page will not throw a timeout exception
        // note that is only a hint we need to have stateless components on the
        // page for this to work, like a statelessform
        setStatelessHint(true);
//        add(new FeedbackPanel("feedback") {
//
//            private static final long serialVersionUID = 1L;
//
//            public boolean isVisible() {
//                return anyMessage();
//            }
//        });
        logoff = new LogOffLink("logoff");

        signPanel = new SignPanel("signInPanel") {
            @Override
            public boolean signIn(String userName, String password) throws AccountIsNotActivatedException, Exception {
                boolean b = super.signIn(userName, password);
                if (b) {
                    logoff.setVisible(true);
                    signPanel.setVisible(false);
                    pageLink.setVisible(false);
                    registrationLink.setVisible(false);
                }
                return b;

            }
        };
        add(signPanel);
        add(logoff);
        pageLink = new BookmarkablePageLink("resetPasswordLink", PasswordResetPage.class);
        add(pageLink);
        registrationLink = new BookmarkablePageLink("registrationLink", RegistrationPage.class);
        add(registrationLink);


        if (!((RolesSession) getSession()).isSignedIn()) {
            logoff.setVisible(false);
            signPanel.setVisible(true);
        } else {
            logoff.setVisible(true);
            signPanel.setVisible(false);
            pageLink.setVisible(false);
            registrationLink.setVisible(false);
        }
    }


    /**
     * Allows subclasses to specify which context should be used when logging
     * off
     *
     * @return the context
     */
    /*protected final LoginContext getLogoffContext() {
    Application app = Application.get();
    if (app instanceof WicketApplication) {
    return ((WicketApplication) app).getLogoffContext();
    }
    throw new WicketRuntimeException("Application is not a subclass of " + WicketApplication.class);
    }*/
    @Override
    public IModel getPageTitle() {
        return new Model("LoginPage");
    }

    @Override
    public IModel getDescription() {
        return new Model("LoginPage");
    }

    @Override
    public IModel getKeywords() {
        return new Model("LoginPage");

    }


    @Override
    protected Component newLeftPanel(String id) {
        return new EmptyPanel(id);
    }

    @Override
    protected Component newRightPanel(String id) {
        return new EmptyPanel(id);
    }


    public void setMySession(MySession mySession) {
        this.mySession = mySession;
    }

    public void setChangePasswordPanel(Panel changePasswordPanel) {
        this.changePasswordPanel = changePasswordPanel;
    }

    public void setResetPasswordPanel(Panel resetPasswordPanel) {
        this.resetPasswordPanel = resetPasswordPanel;
    }
}
